document.oncontextmenu = function () {
    return false;
};

var app = new Vue({
    el: "#app",
    data: {
        hide: false,
        addSwitchDialogIsShow: false,
        updateSwitchDialogIsShow: false,
        allControlSwitch: [],
        optionalFlowmetes: [],
        optionalEquipments: [],
        optionalElectricities: [],
        addSwitchDialogWidth: "20%",
        updateSwitchDialogWidth: "20%",
        addButton: false,
        updateButton: false,
        pagination: {
            currentPage: 1,
            pageSizes: [1, 2, 5, 10, 20, 30, 50, 100],
            pageSize: 10,
            total: 11
        },
        defaultAddAirFlowControlBean: {
            controlSwitchId: "",
            flowmeteId: "",
            electricityMeterId: "",
            autoOnAndOffSwitch: false,
            mainSwitch: false,
            handOperation: false,
            equipmentBeans: [],
            temporalIntervalBeans: [
                [new Date(2000, 1, 1, 0, 0), new Date(2000, 1, 1, 0, 0)]
            ]

        },
        addAirFlowControlBean: {
            controlSwitchId: "",
            flowmeteId: "",
            electricityMeterId: "",
            autoOnAndOffSwitch: false,
            mainSwitch: false,
            handOperation: false,
            equipmentBeans: [],
            temporalIntervalBeans: [
                [new Date(2000, 1, 1, 0, 0), new Date(2000, 1, 1, 0, 0)]
            ]
        },
        updateAirFlowControlBean: {
            airFlowControlId: "",
            controlSwitchId: "",
            flowmeteId: "",
            electricityMeterId: "",
            autoOnAndOffSwitch: false,
            mainSwitch: false,
            handOperation: false,
            equipmentBeans: [],
            temporalIntervalBeans: []
        }
    },
    methods: {
        addSwitchControl: function () {
            this.cleanSelectOptional();
            this.setAllSelectOptional();
            this.addAirFlowControlBean = _.cloneDeep(this.defaultAddAirFlowControlBean);
            this.addSwitchDialogIsShow = true;
        },
        addTemporalIntervalToAddAirFlowControlBean: function (index) {
            this.addAirFlowControlBean.temporalIntervalBeans.splice(index + 1, 0, [new Date(2000, 1, 1, 0, 0), new Date(2000, 1, 1, 0, 0)]);
        },
        delTemporalIntervalFromAddAirFlowControlBean: function (index) {
            if (this.addAirFlowControlBean.temporalIntervalBeans.length > 1) {
                this.addAirFlowControlBean.temporalIntervalBeans.splice(index, 1);
            }
        },
        cancelAddSwitchControl: function () {
            this.addSwitchDialogIsShow = false;
        },
        submitAddSwitchControl: function () {
            this.addButton = true;
            var timeIntervals = _.cloneDeep(this.addAirFlowControlBean.temporalIntervalBeans);
            this.addAirFlowControlBean.temporalIntervalBeans = [];
            for (var i = 0; i < timeIntervals.length; i++) {
                this.addAirFlowControlBean.temporalIntervalBeans.push({
                    "startTime": moment(timeIntervals[i][0]).format('YYYY-MM-DD HH:mm:ss'),
                    "endTime": moment(timeIntervals[i][1]).format('YYYY-MM-DD HH:mm:ss')
                });
            }
            this.addAirFlowControlBean.equiptments = JSON.stringify(this.addAirFlowControlBean.equipmentBeans);
            this.addAirFlowControlBean.temporalInterval = JSON.stringify(this.addAirFlowControlBean.temporalIntervalBeans);
            var self = this;
            ajaxRequest('airFlowControl/addAirFlowControl', this.addAirFlowControlBean).then(function (response) {
                self.$message({
                    message: '添加总开关成功',
                    type: 'success'
                });
                self.setAllData(self.pagination);
            }).catch(function () {
                self.$message.error('添加总开关失败');
            });

            this.addSwitchDialogIsShow = false;
            this.addButton = false;
        },
        updateSwitchControl: function (row, event) {
            this.cleanSelectOptional();
            this.optionalEquipments = _.concat(this.optionalEquipments, row.equipmentBeans);
            this.setAllSelectOptional();
            this.updateAirFlowControlBean.airFlowControlId = row.airFlowControlId;
            this.updateAirFlowControlBean.controlSwitchId = _.trim(row.controlSwitchId);
            this.updateAirFlowControlBean.temporalIntervalBeans = row.temporalIntervalBeans;
            this.updateAirFlowControlBean.equipmentBeans = _.map(row.equipmentBeans, "id");
            this.updateAirFlowControlBean.flowmeteId = row.flowmeteId;
            this.updateAirFlowControlBean.electricityMeterId = row.electricityMeterId;
            this.updateAirFlowControlBean.autoOnAndOffSwitch = row.autoOnAndOffSwitch;
            this.updateAirFlowControlBean.mainSwitch = row.mainSwitch;
            this.updateAirFlowControlBean.handOperation = row.handOperation;
            this.updateSwitchDialogIsShow = true;
        },
        addTemporalIntervalToUpdateAirFlowControlBean: function (index) {
            this.updateAirFlowControlBean.temporalIntervalBeans.splice(index + 1, 0, [new Date(2000, 1, 1, 0, 0), new Date(2000, 1, 1, 0, 0)]);
        },
        delTemporalIntervalFromUpdateAirFlowControlBean: function (index) {
            if (this.updateAirFlowControlBean.temporalIntervalBeans.length > 1) {
                this.updateAirFlowControlBean.temporalIntervalBeans.splice(index, 1);
            }
        },
        cancelUpdateSwitchControl: function () {
            this.updateSwitchDialogIsShow = false;
        },
        submitUpdateSwitchControl: function () {
            this.updateButton = true;
            var timeIntervals = _.cloneDeep(this.updateAirFlowControlBean.temporalIntervalBeans);
            this.updateAirFlowControlBean.temporalIntervalBeans = [];
            for (var i = 0; i < timeIntervals.length; i++) {
                this.updateAirFlowControlBean.temporalIntervalBeans.push({
                    "startTime": moment(timeIntervals[i][0]).format('YYYY-MM-DD HH:mm:ss'),
                    "endTime": moment(timeIntervals[i][1]).format('YYYY-MM-DD HH:mm:ss')
                });
            }
            this.updateAirFlowControlBean.equiptments = JSON.stringify(this.updateAirFlowControlBean.equipmentBeans);
            this.updateAirFlowControlBean.temporalInterval = JSON.stringify(this.updateAirFlowControlBean.temporalIntervalBeans);
            var self = this;
            ajaxRequest('airFlowControl/updateAirFlowControl', this.updateAirFlowControlBean).then(function (response) {
                self.$message({
                    message: '更新总开关成功',
                    type: 'success'
                });
                self.setAllData(self.pagination);
            }).catch(function () {
                self.$message.error('更新总开关失败');
            });
            this.updateSwitchDialogIsShow = false;
            this.updateButton = false;
        },
        toDelAirFlowControl: function (row) {
            this.$confirm('请确认您是否需要删除--->' + row.controlSwitchId + '<---控制开关？', '确认删除信息', {
                    distinguishCancelAndClose: true,
                    confirmButtonText: '确认删除',
                    cancelButtonText: '取消删除'
                })
                .then(function () {
                    var self = this;
                    ajaxRequest('airFlowControl/delAirFlowControl', {
                        airFlowControlId: row.airFlowControlId
                    }).then(function (response) {
                        self.$message({
                            type: 'info',
                            message: '删除成功'
                        });
                        self.setAllData(self.pagination);
                    }).catch(function () {
                        self.$message.error('删除失败');
                    });
                })
                .catch(function () {
                    this.$message({
                        type: 'info',
                        message: '取消删除'
                    })
                });
        },
        getEquipmentBeans: function (response) {
            var result = new Array();
            for (var j = 0; j < response.equipmentBeans.length; j++) {
                result.push({
                    id: response.equipmentBeans[j].id,
                    equipmentName: response.equipmentBeans[j].equipmentName
                });
            }
            return result;
        },
        getTemporalIntervalBeans: function (response) {
            var result = new Array();
            for (var k = 0; k < response.temporalIntervalBeans.length; k++) {
                result.push([moment(response.temporalIntervalBeans[k].startTime), moment(response.temporalIntervalBeans[
                    k].endTime)]);
            }
            return result;
        },
        setAllData: function (pagination) {
            var self = this;
            ajaxRequest('airFlowControl/selectAirFlowControlByPage', pagination, false).then(function (res) {
                self.allControlSwitch = [];
                var response = res.data.returnData;
                self.pagination.total = res.data.iTotalRecords;
                for (var i = 0; i < response.length; i++) {
                    self.allControlSwitch.push({
                        airFlowControlId: response[i].airFlowControlId,
                        autoOnAndOffSwitch: response[i].autoOnAndOffSwitch == 1 ? true : false,
                        controlSwitchId: response[i].controlSwitchId,
                        electricityMeterId: response[i].electricityMeterId,
                        electricityMeterNo: response[i].electricityMeterNo,
                        flowmeteNo: response[i].flowmeteNo,
                        equipmentBeans: self.getEquipmentBeans(response[i]),
                        flowmeteId: response[i].flowmeteId,
                        handOperation: response[i].handOperation == 1 ? true : false,
                        mainSwitch: response[i].mainSwitch == "ON" ? true : false,
                        temporalIntervalBeans: self.getTemporalIntervalBeans(response[i])
                    });
                }
                console.info(self.allControlSwitch);
            }).catch(function (e) {
                console.info(e);
            });
        },
        handleSizeChange(pageSize) {
            this.pagination.pageSize = pageSize;
            this.setAllData(this.pagination);
        },
        handleCurrentChange(currentPage) {
            this.pagination.currentPage = currentPage;
            this.setAllData(this.pagination);
        },
        setAllSelectOptional: function () {
            var self = this;
            /* 请求获取设备信息 */
            ajaxRequest('equipment/getNoMainSwitchEquipment').then(function (response) {
                self.optionalEquipments = _.concat(self.optionalEquipments, response);
            }).catch(function () {
                self.$message.error('获取设备信息失败');
            });
            /* 请求获取未占用的流量计号信息 */
            ajaxRequest('energy/getLeisureFlowEnergy').then(function (response) {
                self.optionalFlowmetes = response;
            }).catch(function () {
                self.$message.error('获取空闲流量计失败');
            });
            /* 请求获取未占用的电表信息 */
            ajaxRequest('energy/getLeisureElectricEnergy').then(function (response) {
                self.optionalElectricities = response;
            }).catch(function () {
                self.$message.error('获取空闲电表计失败');
            });
        },
        cleanSelectOptional: function () {
            this.optionalFlowmetes = [];
            this.optionalEquipments = [];
            this.optionalElectricities = [];
        },
        addTemporalInterval: function () {
            this.addAirFlowControlBean.temporalIntervalBeans.splice(index + 1, 0, [new Date(2000, 1, 1, 0, 0), new Date(2000, 1, 1, 0, 0)]);

        },
        delTemporalInterval: function () {
            if (this.addAirFlowControlBean.temporalIntervalBeans.length > 1) {
                this.addAirFlowControlBean.temporalIntervalBeans.splice(index, 1);
            }
        }
    },
    created: function () {
        this.setAllData(this.pagination);
    }

});